//调用js
$(function(){
   //弹窗
   $('#myModal').modal();
   var acctAddr;
   //构造web3对象
   var web3 = new Web3(new Web3.providers.HttpProvider("http://127.0.0.1:8545"));
   web3.eth.getAccounts(function(e,r){
      console.log(e,r);
   });
   //构造crowd合约对象
   var crowdContractAddr = "0x259272473a8899032a6afff4a01eb7f9319a1df9";
   var ownerAddr = "0x5a61e77544bb569cf7e5d2739a03db1e21b49aff";
   var crowdObj = new web3.eth.Contract(crowdABI, crowdContractAddr);

   //get insideList
   var mvcContractAddr;
   var mvcObj;
   function getInsideList(){
      crowdObj.methods.insideList().call(function(error,result){
         if(!error){
            //获取mvc合约地址
            mvcContractAddr = result.mvcContractAddr;
            console.log("get mvc addr:",mvcContractAddr);
            //获取mvc合约对象
            mvcObj = new web3.eth.Contract(mvcABI, mvcContractAddr);
         }
      });

   }
   getInsideList();

   //登陆按钮被点击
   $(".Login").on("click",function(){
      acctAddr = $("#addressId").val();
      console.log("get acct:",acctAddr);
      $(".close_win").click();
   });


   //购票
   $(".Buy").on("click",function(){
      //调用mvc合约中的ticketBooking方法
      mvcObj.methods.ticketBooking().send({
         from  : acctAddr,
         value : 20000000000000000,
         gas   : 300000
      },function(error,result){
         if(!error){
            alert("购票成功！");
         }else{
            alert("购票失败！");
            console.log("购票失败:",error);
         }
      });
   });

   

});